package com.zhanghe.study.java8.time;

import java.time.*;
import java.time.format.DateTimeFormatter;

/**
 * java8新的时间
 * LocalDate LocalTime LocalDateTime
 */
public class TestLocalDateTime {
    public static void main(String[] args) {
        System.out.println("=============testLocalDateTime========");
        testLocalDateTime();
        System.out.println("=============testInstant========");
        testInstant();
        System.out.println("=============testDuration========");
        testDuration();
        System.out.println("=============testPeriod========");
        testPeriod();
        System.out.println("=============testDateTimeFormatter========");
        testDateTimeFormatter();

    }

    public static void testLocalDateTime(){
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println("当前时间"+localDateTime);

        LocalDateTime localDateTime1 = LocalDateTime.of(2020,11,6,9,0,0);
        System.out.println("确认时间"+localDateTime1);

        LocalDateTime localDateTime2 = localDateTime1.plusDays(1);
        System.out.println("加一天"+localDateTime2);
        LocalDateTime localDateTime3 = localDateTime1.minusDays(1);
        System.out.println("减去一天"+localDateTime3);
    }

    /**
     * UTC时间
     */
    public static void testInstant(){
        // 默认获取UTC时区
        Instant instant = Instant.now();
        System.out.println("UTC时间"+instant);
        OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
        System.out.println("东八区时间"+offsetDateTime);
        System.out.println("时间戳"+instant.toEpochMilli());
    }

    /**
     * 时间间隔
     */
    public static void testDuration(){
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println("当前时间"+localDateTime);

        LocalDateTime localDateTime1 = localDateTime.minusSeconds(5);
        System.out.println("减去5s"+localDateTime1);
        Duration duration = Duration.between(localDateTime,localDateTime1);
        System.out.println("相差"+duration.toMillis());
    }

    /**
     * 日期间隔
     */
    public static void testPeriod(){
        LocalDate localDate = LocalDate.now();
        System.out.println("当前日期"+localDate);

        LocalDate localDate1 = localDate.minusDays(5);
        System.out.println("减去5天"+localDate1);
        Period period = Period.between(localDate,localDate1);
        System.out.println("相差"+period.getDays());
    }

    /**
     * 时间格式化
     */
    public static void testDateTimeFormatter(){
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDateTime localDateTime = LocalDateTime.now();
        String date = dateTimeFormatter.format(localDateTime);
        System.out.println("时间格式化"+date);
    }


}
